package com.newway.back.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.newway.back.entity.CoachEntity;
import com.newway.back.mapper.CoachMapper;
import com.newway.common.result.Result;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/system/coach")
public class CoachController {

    @Resource
    CoachMapper coachMapper;

    @GetMapping("/list")
    private Result getList(CoachEntity coachEntity){
        Page<CoachEntity> coachEntityPage = new Page<>(coachEntity.getPageNum(),coachEntity.getPageSize());
        QueryWrapper<CoachEntity> coachEntityQueryWrapper = new QueryWrapper<>();
        Page<CoachEntity> result = coachMapper.selectPage(coachEntityPage, coachEntityQueryWrapper);
        return Result.success(result);
    }

    @GetMapping("/getList")
    private Result wechat(CoachEntity coachEntity){
        Page<CoachEntity> coachEntityPage = new Page<>(coachEntity.getPageNum(),coachEntity.getPageSize());
        QueryWrapper<CoachEntity> coachEntityQueryWrapper = new QueryWrapper<>();
        if(StringUtils.isNotBlank(coachEntity.getName())){
            coachEntityQueryWrapper.like("name",coachEntity.getName());
            coachEntityQueryWrapper.or();
            coachEntityQueryWrapper.like("location",coachEntity.getName());
        }
        if(coachEntity.getPrice()!=null){
            coachEntityQueryWrapper.orderByAsc("price");
        }
        if(coachEntity.getRating()!=null){
            coachEntityQueryWrapper.orderByDesc("rating");
        }
        Page<CoachEntity> result = coachMapper.selectPage(coachEntityPage, coachEntityQueryWrapper);
        return Result.success(result);
    }

    @PostMapping("/addOrUpdate")
    private Result addOrUpdate(@RequestBody CoachEntity coachEntity){
        coachMapper.insertOrUpdate(coachEntity);
        return Result.success();
    }

    @PostMapping("/delete/{id}")
    private Result addOrUpdate(@PathVariable("id") Integer id){
        coachMapper.deleteById(id);
        return Result.success();
    }

    @PostMapping("/details/{id}")
    private Result details(@PathVariable("id") Integer id){
        CoachEntity coachEntity = coachMapper.selectById(id);
        return Result.success(coachEntity);
    }
}
